package com.vates.reservas.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.vates.reservas.dominio.Hotel;
import com.vates.reservas.dominio.TipoHabitacion;
import com.vates.reservas.service.HotelService;

/**
 * Handles and retrieves the main requests
 */
@Controller
@RequestMapping(value = "/habitacion/editarHabitacion")
public class HabitacionEditarController {

	protected static Logger logger = Logger.getLogger("controller");

	@Autowired
	private HotelService hotelService;

	/**
	 * Handles and retrieves the AJAX Add page
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String gethotelEditar(Model model) {
		model.addAttribute(new Hotel());
		return "habitacion/editarHabitacion";
	}

	@RequestMapping(method=RequestMethod.POST)
	public String create(@Valid Hotel hotel, BindingResult result) {
		if (result.hasErrors()) {
			return "habitacion/editarHabitacion";
		}
		return "redirect:/habitacion/editarHabitacion/" + hotel.getId();
	}

	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public String gethotelModificar(@PathVariable Long id, Model model) {
		Hotel hotel= hotelService.busquedaPorId(id);
		model.addAttribute(hotel);
		model.addAttribute(new TipoHabitacion());
		return "habitacion/editarSeleccionarHabitacion";
	}
	@RequestMapping(method=RequestMethod.POST, value="/editar/seleccionar")
	public String seleccionar(HttpServletRequest request, Model model) {
		long idHotel = Long.parseLong(request.getParameter("nroHotel"));
		long idHabitacion = Long.parseLong(request.getParameter("radio"));
		Hotel hotel= hotelService.busquedaPorId(idHotel);
		model.addAttribute(hotel);
		TipoHabitacion alModel= new TipoHabitacion();
		List<TipoHabitacion> habitaciones = hotel.getHabitaciones();
		for(TipoHabitacion habitacion : habitaciones) {
			if(habitacion.getId()== idHabitacion) {
				alModel = habitacion;
			}
		}
		model.addAttribute(alModel);
		return "habitacion/editar";
	}
	@RequestMapping(method=RequestMethod.POST, value="/editar/modificar")
	public String modificar(@Valid TipoHabitacion tipoHabitacion, HttpServletRequest request) {

		long idHotel = Long.parseLong(request.getParameter("nroHotel"));
		long idHabitacion = tipoHabitacion.getId();
		String nombreHabitacion = tipoHabitacion.getNombre();
		String descripcionHabitacion = tipoHabitacion.getDescripcion();
		int cantidadHabitacion = tipoHabitacion.getCantidad();
		int maxOcupantesHabitacion = tipoHabitacion.getMaxOcupantes();
		double precioHabitacion = tipoHabitacion.getPrecio();
		hotelService.modificarHabitacion(idHotel, idHabitacion, nombreHabitacion, descripcionHabitacion, cantidadHabitacion, maxOcupantesHabitacion, precioHabitacion);
		return "redirect:/hotel/busquedaPorId/"+idHotel;
	}
}